/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package proyecto.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.view.RedirectView;
import proyecto.dao.MedicoDAO;
import proyecto.modelo.Especialidad;
import proyecto.modelo.Medico;

/** 
 *
 * @author Administrador
 */
public class MedicoController extends MultiActionController {

    public MedicoController() {
        //Initialize controller properties here or 
        //in the Web Application Context

        //setCommandClass(MyCommand.class);
        //setCommandName("MyCommandName");
        //setSuccessView("successView");
        //setFormView("formView");
    }
    
    //@Override
    protected void doSubmitAction(Object command) throws Exception {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    private MedicoDAO medicoDAO;

    public MedicoDAO getMedicoDAO() {
        return medicoDAO;
    }

    public void setMedicoDAO(MedicoDAO medicoDAO) {
        this.medicoDAO = medicoDAO;
    }

    public ModelAndView doInsertar(HttpServletRequest request, HttpServletResponse response) {
        Medico obj = new Medico();
        obj.setDni(request.getParameter("dni"));
        obj.setNombre(request.getParameter("nombre"));
        obj.setApellido(request.getParameter("apellido"));
        String fecha = request.getParameter("nacimiento");
        //paso de string a formato date
        DateFormat myDateFormat = new SimpleDateFormat(fecha);
        Date myDate = null;
        try {
             myDate = myDateFormat.parse(fecha);
        } catch (Exception e) {
             System.out.println("Invalid Date Parser Exception ");
             e.printStackTrace();
        }
        //
        obj.setFechaNac(myDate);
        Especialidad especialidad = new Especialidad();
        especialidad.setCodigo(request.getParameter("especialidad"));        
        obj.setEspecialidad(especialidad);
        obj.setNumColegiatura(request.getParameter("colegiatura"));
        obj.setDireccion(request.getParameter("direccion"));
        obj.setTelefono(request.getParameter("telefono"));
        obj.setEmail(request.getParameter("mail"));
        obj.setSexo(request.getParameter("sexo"));

        medicoDAO.insertar(obj);

       return new ModelAndView(new RedirectView("confirmar-datos-paciente.htm", true));
    }

    //Use onSubmit instead of doSubmitAction 
    //when you need access to the Request, Response, or BindException objects
    /*
    @Override
    protected ModelAndView onSubmit(
            HttpServletRequest request, 
            HttpServletResponse response, 
            Object command, 
            BindException errors) throws Exception {
        ModelAndView mv = new ModelAndView(getSuccessView());
        //Do something...
        return mv;
    }
    */

}